﻿#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using PdfCreative.Pdf.Writer.Embedding.Decoders;
using PdfCreative.Pdf.Writer.Utilities;

namespace PdfCreative.Pdf.Writer.Embedding
{
    /// <summary>
    /// The DecoderStream class is a stream that decodes data contained in another stream. The DecoderStream class
    /// is abstract. Inheriting classes implement specific decoding functionality.
    /// </summary>
    internal abstract class DecoderStream : Stream
    {
        // Private fields
        // ==============
        #region Private fields
        private Stream _baseStream;
        #endregion



        // Public methods
        // ==============
        #region Public methods
        /// <summary>
        /// Retrieves a decoder stream for the given decoding method. If no decoder stream for the
        /// decoding method could be found, this method returns a Null reference.
        /// </summary>
        /// <param name="method">The decoding method.</param>
        /// <returns>A reference to a suitable decoder stream.</returns>
        public static DecoderStream GetDecoder(string method)
        {
            return 
                ReflectionUtilities.GetMatchingInstance<DecoderStream, DecoderAttribute>(
                delegate(DecoderAttribute attr) { return attr.Method == method; });
        }

        /// <summary>
        /// Retrieves the stream that the DecoderStream wraps.
        /// </summary>
        public Stream Stream
        {
            get { return _baseStream; }
        }

        /// <summary>
        /// In inheriting classes, initialises the DecoderStream with the specified base stream and parameters dictionary.
        /// </summary>
        /// <param name="baseStream">The base stream</param>
        /// <param name="parameters">The parameters for this decoder stream</param>
        public abstract void Initialise(Stream baseStream, PdfDictionary parameters);
        #endregion



        // Base class overrides
        // ====================
        #region Base class overrides
        /// <summary>
        /// Indicates if this stream can read data.
        /// </summary>
        public override bool CanRead
        {
            get { return true; }
        }

        /// <summary>
        /// Indicates if this stream can seek.
        /// </summary>
        public override bool CanSeek
        {
            get { return false; }
        }

        /// <summary>
        /// Indicates if this stream can write.
        /// </summary>
        public override bool CanWrite
        {
            get { return false; }
        }

        /// <summary>
        /// Flushes the stream. This is not supported on decoders streams.
        /// </summary>
        public override void Flush()
        {
            throw new NotSupportedException("Writing is not supported on a decoder stream");
        }

        /// <summary>
        /// Retrieves the length of the stream.
        /// </summary>
        public override long Length
        {
            get { return _baseStream.Length; }
        }

        /// <summary>
        /// Sets or retrieves the position of the stream. Setting the position is not supported on decoder
        /// streams.
        /// </summary>
        public override long Position
        {
            get
            {
                return _baseStream.Position;
            }
            set
            {
                throw new NotSupportedException("Seeking is not supported on decoder streams");
            }
        }

        /// <summary>
        /// Seeks the stream. Seeking is not supported on decoder streams.
        /// </summary>
        /// <param name="offset">The offset to seek by.</param>
        /// <param name="origin">Te origin to seek from.</param>
        /// <returns>The new position</returns>
        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotSupportedException("Seeking is not supported on decoder streams");
        }

        /// <summary>
        /// Sets the length of the stream. This is not supported on decoder streams.
        /// </summary>
        /// <param name="value">The new length.</param>
        public override void SetLength(long value)
        {
            throw new NotSupportedException("This is not supported on a decoder stream");
        }

        /// <summary>
        /// Writes the data in the buffer to the stream.
        /// </summary>
        /// <param name="buffer">The data to write</param>
        /// <param name="offset">The offset at which to start copying</param>
        /// <param name="count">The number of bytes to write</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException("Writing is not supported on a decoder stream");
        }
        #endregion



        // Protected implementation
        // ========================
        #region Protected implementation
        /// <summary>
        /// Sets the base stream to the specified instance.
        /// </summary>
        /// <param name="stream">The stream to use as the base stream.</param>
        protected void SetStream(Stream stream)
        {
            _baseStream = stream;
        }
        #endregion

    } // DecoderStream class
}
